<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>melgmm.m</title>
<link rel="stylesheet" type="text/css" href="../../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>model</span>=<span class=defun_name>melgmm</span>(<span class=defun_in>X,Alpha,cov_type</span>)<br>
<span class=h1>%&nbsp;MELGMM&nbsp;Maximizes&nbsp;Expectation&nbsp;of&nbsp;Log-Likelihood&nbsp;for&nbsp;Gaussian&nbsp;mixture.</span><br>
<span class=help>%&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;melgmm(X,Alpha)</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;melgmm(X,Alpha,cov_type)</span><br>
<span class=help>%&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;melgmm(X,Alpha)&nbsp;maximizes&nbsp;expectation&nbsp;of&nbsp;log-likelihood&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;function&nbsp;for&nbsp;Gaussian&nbsp;mixture&nbsp;model</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;(Mean,Cov,Prior)&nbsp;=&nbsp;&nbsp;argmax&nbsp;&nbsp;F(Mean,Cov,Prior)</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mean,Cov,Prior&nbsp;</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;where</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;F&nbsp;=&nbsp;sum&nbsp;sum&nbsp;Alpha(j,i)*log(pdfgauss(X(:,i),Mean(:,y),Cov(:,:,y)))</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;&nbsp;&nbsp;i&nbsp;</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;The&nbsp;solution&nbsp;is&nbsp;returned&nbsp;in&nbsp;the&nbsp;structure&nbsp;model&nbsp;with&nbsp;fields</span><br>
<span class=help>%&nbsp;&nbsp;Mean&nbsp;[dim&nbsp;x&nbsp;ncomp],&nbsp;Cov&nbsp;[dim&nbsp;x&nbsp;dim&nbsp;x&nbsp;ncomp]&nbsp;and&nbsp;Prior&nbsp;[1&nbsp;x&nbsp;ncomp].</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;melgmm(X,Alpha,cov_type)&nbsp;specifies&nbsp;covariance&nbsp;matrix:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;cov_type&nbsp;=&nbsp;'full'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;full&nbsp;covariance&nbsp;matrix&nbsp;(default)</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;cov_type&nbsp;=&nbsp;'diag'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;diagonal&nbsp;covarinace&nbsp;matrix</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;cov_type&nbsp;=&nbsp;'spherical'&nbsp;spherical&nbsp;covariance&nbsp;matrix</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Data&nbsp;sample.</span><br>
<span class=help>%&nbsp;&nbsp;Alpha&nbsp;[ncomp&nbsp;x&nbsp;num_data]&nbsp;Distribution&nbsp;of&nbsp;hidden&nbsp;state&nbsp;given&nbsp;sample.</span><br>
<span class=help>%&nbsp;&nbsp;cov_type&nbsp;[string]&nbsp;Type&nbsp;of&nbsp;covariacne&nbsp;matrix&nbsp;(see&nbsp;above).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;[struct]&nbsp;Gaussian&nbsp;mixture&nbsp;model:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Mean&nbsp;[dim&nbsp;x&nbsp;ncomp]&nbsp;Mean&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Cov&nbsp;[dim&nbsp;x&nbsp;dim&nbsp;x&nbsp;ncomp]&nbsp;Covariance&nbsp;matrices.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Prior&nbsp;[1&nbsp;x&nbsp;ncomp]&nbsp;Distribution&nbsp;of&nbsp;hidden&nbsp;state.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;EMGMM,&nbsp;MLCGMM.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2003,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;30-apr-2004,&nbsp;VF</span><br>
<span class=help1>%&nbsp;19-sep-2003,&nbsp;VF</span><br>
<span class=help1>%&nbsp;27-feb-2003,&nbsp;VF</span><br>
<br>
<hr>
<span class=comment>%&nbsp;Processing&nbsp;of&nbsp;input&nbsp;arguments&nbsp;</span><br>
<span class=comment>%----------------------------------------</span><br>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;&lt;&nbsp;3,&nbsp;cov_type&nbsp;=&nbsp;<span class=quotes>'full'</span>;&nbsp;<span class=keyword>end</span><br>
[dim,&nbsp;num_data]&nbsp;=&nbsp;size(&nbsp;X&nbsp;);<br>
&nbsp;&nbsp;<br>
<span class=comment>%&nbsp;------------------------------------</span><br>
ncomp&nbsp;=&nbsp;size(Alpha,1);<br>
<br>
model.Mean&nbsp;=&nbsp;zeros(dim,ncomp);<br>
model.Cov&nbsp;=&nbsp;zeros(dim,dim,ncomp);<br>
<br>
<span class=keyword>for</span>&nbsp;i=1:ncomp,<br>
<br>
&nbsp;&nbsp;nconst&nbsp;=&nbsp;sum(&nbsp;Alpha(i,:)&nbsp;);<br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;~nconst,<br>
&nbsp;&nbsp;&nbsp;&nbsp;model.Mean(:,i)&nbsp;=&nbsp;NaN*ones(dim,1);<br>
&nbsp;&nbsp;&nbsp;&nbsp;model.Cov(:,:,i)&nbsp;=&nbsp;NaN*ones(dim,dim);<br>
&nbsp;&nbsp;&nbsp;&nbsp;model.Prior(i)&nbsp;=&nbsp;0;<br>
&nbsp;&nbsp;<span class=keyword>else</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;model.Mean(:,i)&nbsp;=&nbsp;X*Alpha(i,:)'/nconst;<br>
&nbsp;&nbsp;&nbsp;&nbsp;XC&nbsp;=&nbsp;X&nbsp;-&nbsp;model.Mean(:,i)*ones(1,num_data);<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>switch</span>&nbsp;cov_type,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=label>case</span>&nbsp;<span class=quotes>'full'</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.Cov(:,:,i)&nbsp;=&nbsp;(XC.*(repmat(Alpha(i,:),dim,1)))*XC'/nconst;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=label>case</span>&nbsp;<span class=quotes>'diag'</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.Cov(:,:,i)=diag(sum(XC.*(ones(dim,1)*Alpha(i,:)).*XC,2))/nconst;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=label>case</span>&nbsp;<span class=quotes>'spherical'</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.Cov(:,:,i)&nbsp;=&nbsp;eye(dim)*...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sum(sum(XC.^2.*(ones(dim,1)*Alpha(i,:))&nbsp;))/(nconst*dim);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=label>otherwise</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=error>error</span>(<span class=quotes>'Wrong&nbsp;cov_type.'</span>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;model.Prior(i)&nbsp;=&nbsp;nconst/num_data;<br>
&nbsp;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>end</span>&nbsp;&nbsp;<br>
&nbsp;&nbsp;<br>
<br>
<span class=jump>return</span>;&nbsp;<br>
</code>
